package com.dotstone.chipism.activity;

import java.util.ArrayList;
import java.util.List;

import com.dotstone.chipism.R;
import com.dotstone.chipism.bean.Device;
import com.dotstone.chipism.bean.DeviceType;
import com.dotstone.chipism.bean.MySocket;
import com.dotstone.chipism.dialog.SVProgressHUD;
import com.dotstone.chipism.util.CheckStatusUtil;
import com.dotstone.chipism.util.DeviceManager;
import com.dotstone.chipism.util.SocketManager;
import com.dotstone.chipism.util.ToastShow;
import com.dotstone.chipism.view.BlurBehind;
import com.dotstone.chipism.view.slidingfragmenu.BarUtils;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class DeviceStateActivity extends BaseActivity {
	private TextView tv_statusBar_mian;
	private LinearLayout mBackL;
	private ExpandableListView mWlanLv;
	private List<Device> wlans = new ArrayList<Device>();
	// private WlanAdapter wlanAdapter;
	private WlanParentAdapter wlanAdapter;
	private SVProgressHUD mSvprogressHUD;
	private List<Device> mSwitchs = new ArrayList<Device>();
	private List<Device> mSockets = new ArrayList<Device>();
	private List<Device> mLights = new ArrayList<Device>();
	private List<Device> mCameras = new ArrayList<Device>();

	@Override
	public void widgetClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back_layout:
			finish();
			break;

		default:
			break;
		}
	}

	Handler mHandler = new Handler() {
		public void handleMessage(final Message msg) {
			switch (msg.what) {
			case 13:
				Log.i("wmy", "全部获取完毕");
				if (mSvprogressHUD.isShowing()) {
					mSvprogressHUD.dismiss();
				}
				ToastShow.Show(getApplicationContext(), "状态更新成功");
				wlanAdapter.notifyDataSetChanged();
				SocketManager.getInstance().setNeedToCheck(false);
				break;
			case 14:
				wlanAdapter.notifyDataSetChanged();
				break;
			}

		}
	};

	@Override
	public void initParms(Bundle parms) {
		// TODO Auto-generated method stub

	}

	@Override
	public View bindView() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int bindLayout() {
		// TODO Auto-generated method stub
		return R.layout.activity_device_state;
	}

	@Override
	public void initView(View view) {
		// TODO Auto-generated method stub
		BlurBehind.getInstance().setBackground(this);
		mSvprogressHUD = new SVProgressHUD(DeviceStateActivity.this);
		tv_statusBar_mian = $(R.id.tv_statusBar_mian);
		mBackL = $(R.id.back_layout);
		mWlanLv = $(R.id.wlan_lv);
		boolVersion();
		mBackL.setOnClickListener(this);
		initData();
	}

	private void initData() {
		// TODO Auto-generated method stub
		// mSvprogressHUD.showWithStatus("正在获取设备状态");
		DeviceManager.getInstance().setChildDevice();
		wlans = DeviceManager.getInstance().getWlans();
		wlans.addAll(DeviceManager.getInstance().getCameras());
		wlanAdapter = new WlanParentAdapter();
		mWlanLv.setAdapter(wlanAdapter);
		mWlanLv.setOnGroupExpandListener(new OnGroupExpandListener() {

			@Override
			public void onGroupExpand(int groupPosition) {
				// TODO Auto-generated method stub
				Log.i("wmy", "展开 groupPosition = " + groupPosition);
				// 监听到某个类型的设备展开 需要查询状态
				// 遍历该类型的设备
				// 找出所有的mid
				// 根据mid发送查询设备状态的指令 cmd = 87
				DeviceManager.getInstance().setChildDevice();
				wlans = DeviceManager.getInstance().getWlans();
				if (wlanAdapter != null) {
					wlanAdapter.notifyDataSetChanged();
				}
				checkDeviceState(groupPosition);
			}
		});
	}

	private void checkDeviceState(int groupPosition) {
		// mids.clear();
		mSvprogressHUD.showWithStatus("正在获取设备状态");
		SocketManager.getInstance().setNeedToCheck(true);
		CheckStatusUtil.getInstance().mHandler = mHandler;
		CheckStatusUtil.getInstance().check1(groupPosition);
		for (int i = 0; i < wlans.get(groupPosition).getChildDevices().size(); i++) {
			if (wlans.get(groupPosition).getChildDevices().get(i).getCheckCmd().length() != 0) {
				wlans.get(groupPosition).getChildDevices().get(i).checkStatus();
			}
		}
	}

	@Override
	public void setListener() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doBusiness(Context mContext) {
		// TODO Auto-generated method stub

	}

	public void boolVersion() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

			Window window = getWindow();
			window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			// 透明导航栏
			// getWindow().addFlags(
			// WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
			// 改变titlebar的高度
			int statusbarHeight = BarUtils.getStatusBarHeight(this);
			RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) tv_statusBar_mian
					.getLayoutParams();
			layoutParams1.height = statusbarHeight;
			tv_statusBar_mian.setLayoutParams(layoutParams1);
			tv_statusBar_mian.setVisibility(View.VISIBLE);

		} else {
			tv_statusBar_mian.setVisibility(View.GONE);
		}
	}

	class WlanParentAdapter extends BaseExpandableListAdapter {

		// 获得某个父项的某个子项
		@Override
		public Object getChild(int parentPos, int childPos) {
			// TODO Auto-generated method stub
			return wlans.get(parentPos).getChildDevices().get(childPos);
		}

		@Override
		public long getChildId(int arg0, int arg1) {
			// TODO Auto-generated method stub
			return 0;
		}

		// 获得子项显示的view
		@Override
		public View getChildView(int parentPos, int childPos, boolean b, View view, ViewGroup viewGroup) {
			if (view == null) {
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = inflater.inflate(R.layout.item_child_device, null);
			}
			view.setTag(R.layout.item_wlan, parentPos);
			view.setTag(R.layout.item_child_device, childPos);
			if (parentPos < wlans.size()) {
				if (childPos < wlans.get(parentPos).getChildDevices().size()) {
					Device d = wlans.get(parentPos).getChildDevices().get(childPos);

					ImageView type = (ImageView) view.findViewById(R.id.type_img);
					TextView text = (TextView) view.findViewById(R.id.name_tv);
					TextView status = (TextView) view.findViewById(R.id.state_tv);
					text.setText(d.getDeviceName());
					if (DeviceManager.getInstance().checkDeviceState(d.getDeviceID())) {
						status.setText("开");
					} else {
						status.setText("关");
					}
					if (d.getDeviceType() == DeviceType.DEVICE_TYPE_LOCK) {
						type.setImageResource(R.drawable.btn103);
						status.setVisibility(View.GONE);
					} else if (d.getDeviceType() == DeviceType.DEVICE_TYPE_RGB_LIGHT) {
						type.setImageResource(R.drawable.btn105);
					} else if (d.getDeviceType() == DeviceType.DEVICE_TYPE_SOCKET) {
						type.setImageResource(R.drawable.btn107);
					} else if (d.getDeviceType() == DeviceType.DEVICE_TYPE_SWITCH) {
                        type.setImageResource(R.drawable.btn106);
                    }else if (d.getDeviceType() == DeviceType.DEVICE_TYPE_CURTAIN) {
					    type.setImageResource(R.drawable.ic_curtain);
					} else {
						status.setVisibility(View.GONE);
					}
				}
			}
			return view;
		}

		// 获得某个父项的子项数目
		@Override
		public int getChildrenCount(int parentPos) {
			// TODO Auto-generated method stub
			if(wlans.get(parentPos).getChildDevices()!=null){
				return wlans.get(parentPos).getChildDevices().size();
			}else{
				return 0;
			}
		}

		@Override
		public Object getGroup(int parentPos) {
			// TODO Auto-generated method stub
			return wlans.get(parentPos);
		}

		// 获得父项的数量
		@Override
		public int getGroupCount() {
			// TODO Auto-generated method stub
			return wlans.size();
		}

		@Override
		public long getGroupId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		// 获得父项显示的view
		@Override
		public View getGroupView(int parentPos, boolean isExpanded, View view, ViewGroup viewGroup) {
			if (view == null) {
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = inflater.inflate(R.layout.item_wlan, null);
			}
			view.setTag(R.layout.item_wlan, parentPos);
			view.setTag(R.layout.item_child_device, -1);
			Device wlan = wlans.get(parentPos);
			TextView name = (TextView) view.findViewById(R.id.name_tv);
			TextView status = (TextView) view.findViewById(R.id.state_tv);
			ImageView biao = (ImageView) view.findViewById(R.id.biao);
			name.setText(wlan.getDeviceName());
			if (isExpanded) {
				biao.setImageResource(R.drawable.biao2);
			} else {
				biao.setImageResource(R.drawable.biao1);
			}
			MySocket socket = SocketManager.getInstance().sMap.get(wlan.getDeviceID());
			if (socket != null) {
				if (socket.isLocalSuccess()) {
					status.setText("本地");
				} else if (!socket.isLocalSuccess() && SocketManager.getInstance().isRemoteSuccess()) {
					if (SocketManager.getInstance().mStatusMap.get(wlan.getDeviceID()) != null) {
						if (SocketManager.getInstance().mStatusMap.get(wlan.getDeviceID()) == 1) {
							status.setText("远程");
						} else {
							status.setText("未连接");
						}
					} else {
						status.setText("未连接");
					}
				} else {
					status.setText("未连接");
				}
			}
			return view;
		}

		@Override
		public boolean hasStableIds() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean isChildSelectable(int arg0, int arg1) {
			// TODO Auto-generated method stub
			return false;
		}
	}

}
